package framework.view.components.container
{
import flash.errors.IllegalOperationError;

import utils.VectorUtil;

/**
 * PaginationListProxy
 * @author jliang
 * 
 */
public class PaginationListProxy extends ListProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function PaginationListProxy()
	{
		super();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	public function get itemRepeater():RepeaterProxy
	{
		return proxys["list"];
	}
	
	protected var currentPage:int = 0;
	
	protected function get preCount():int
	{
		return itemRepeater.items.length;
	}
	
	protected function get repeaterItemRenderClass():Class
	{
		throw new IllegalOperationError("repeaterItemRenderClass getter should be overrided.");
	}
    
    protected function get listSkin():String
    {
        return "list";
    }	
    
	protected var data:*;
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:listSkin, classRef: RepeaterProxy, id:"list", renderProxyRef:repeaterItemRenderClass, onItemSelected:itemSelectedHandler }
		].concat(super.proxyMapping);
	}
	
	override protected function clickToPrevHandler():void
	{
		updateList(data, data.length, currentPage - 1);
	}
	
	override protected function clickToNextHandler():void
	{
		updateList(data, data.length, currentPage + 1);
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function setData(data:*):void
	{
		this.data = data;
		
		updateList(data, data.length, 0);
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	/**
	 * return { data: Object, isEnd:Boolean } 
	 * @param source
	 * @param count
	 * @param index
	 * @param index
	 * @return 
	 * 
	 */	
	protected function getPageData(source:*, count:int, index:int = 0):*
	{		
		var pageCount:int = Math.ceil(count / preCount);
		if (source is Array || VectorUtil.isVector(source))
		{			
			var isEnd:Boolean = index == pageCount - 1;
			if (count <= 0) 
            {
                isEnd = true;
                index = pageCount - 1;
            }
			return { data: source.slice(index * preCount, index * preCount + preCount), isEnd: isEnd};
		}
		else
		{
			throw new IllegalOperationError("Can not identify the class type of source.");
		}
	}
	
	protected function updateList(source:*, count:int, index:int = 0):void
	{
		var obj:* = getPageData(source, count, index);
		
		itemRepeater.data = obj.data;
		currentPage = index;
		
		prevBtn.enabled = currentPage > 0;
		nextBtn.enabled = !obj.isEnd;
	}
    
    protected function goFirstPage(source:*, count:int):void
    {
        updateList(source, count, 0);
    }
    
    protected function goLastPage(source:*, count:int):void
    {
        updateList(source, count, int.MAX_VALUE);
    }
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function itemSelectedHandler(render:IRenderProxy):void
	{
		
	}
}
}